<?php


namespace app\api\model;


use app\lib\exception\recentException;

class Category extends BaseModel {
    protected $hidden = ['delete_time','update_time'];
    //'topic_img_id'

    public function products()
    {
        return $this->hasMany('Product', 'category_id', 'id');
    }

    public function img(){
        return $this->belongsTo('Image','topic_img_id','id');
    }

//    public static function getCategory(){
//        $list = self::with('img')->select();
//        return $list;
//    }

    //获取全部分类
    public static function getCategory(){
        $list = self::select()->toArray();

        foreach ($list as &$value){
            $value['img']= db('image')->field('from,url')->where('id',$value['topic_img_id'])->find();
            $value['img']['url'] = imgUrl($value['img']['url'],$value['img']);
            unset($value['img']['from']);
        }
        return $list;
    }

    //获取一个分类
    public static function getOneCategory($id){
        $list = self::with('products')->find($id)->toArray();
        $list['img'] = db('image')->where('id',$list['topic_img_id'])->field('url,from')->find();
        $list['img']['url'] = imgUrl($list['img']['url'],$list['img']);
        return $list;
    }

}









